package com.baihan.angus.app.game.busshow.ui.main.splash;

import android.Manifest;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import com.alibaba.android.arouter.launcher.ARouter;
import com.baihan.angus.app.game.basic.base.BaseActivity;
import com.baihan.angus.app.game.basic.utils.Loog;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.busshow.databinding.ActivitySplashBinding;
import com.baihan.angus.app.game.common.arouter_path.ArouterAppPath;
import com.baihan.angus.app.game.common.bean.response.Userinfo;
import com.baihan.angus.app.game.common.configs.UserConfig;
import com.google.gson.Gson;
import com.tencent.smtt.export.external.TbsCoreSettings;
import com.tencent.smtt.sdk.QbSdk;
import com.tencent.smtt.sdk.TbsDownloader;
import com.tencent.smtt.sdk.TbsListener;

import java.util.HashMap;

public class SplashActivity extends BaseActivity<ActivitySplashBinding, SplashVM> {
    public static final int PERMISSION_YES = 0xb22;

    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.CAMERA,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE};

    private Userinfo userInfo;

    private boolean permission = false;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_splash;
    }

    @Override
    protected int getViewModelId() {
        return 0;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        //初始化X5内核
//        initTBS();
    }

    @Override
    protected void initData() {
        UserConfig.getUserConfig().initUser();
        new Handler().postDelayed(() -> {
            toJump();
        }, 2000);
    }

    private void toJump() {
        Loog.dTag("UserInfoSplash", "userInfo:" + new Gson().toJson(UserConfig.getUserConfig().getUser()));
        userInfo = UserConfig.getUserConfig().getUser();
        if (null == userInfo) {
            ARouter.getInstance().build(ArouterAppPath.LOGIN_ACTIVITY).navigation();
        } else {
            ARouter.getInstance().build(ArouterAppPath.MAIN_ACTIVITY).navigation();
        }
        finish();
    }

    public void initTBS() {
        HashMap map = new HashMap();
        map.put(TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER, true);
        map.put(TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE, true);
        QbSdk.initTbsSettings(map);

        QbSdk.setDownloadWithoutWifi(true);
        QbSdk.setTbsListener(
                new TbsListener() {
                    @Override
                    public void onDownloadFinish(int i) {
                        //下载结束时的状态，下载成功时errorCode为100,其他均为失败，外部不需要关注具体的失败原因
                        Log.d("QbSdk", "onDownloadFinish -->下载X5内核完成：" + i);
                        if (i != 100) {
                            QbSdk.reset(getApplicationContext());
                            TbsDownloader.startDownload(getApplicationContext());
                            return;
                        }
                    }

                    @Override
                    public void onInstallFinish(int i) {
                        //安装结束时的状态，安装成功时errorCode为200,其他均为失败，外部不需要关注具体的失败原因
                        Log.d("QbSdk", "onInstallFinish -->安装X5内核进度：" + i);
                    }

                    @Override
                    public void onDownloadProgress(int i) {
                        //下载过程的通知，提供当前下载进度[0-100]
                        Log.d("QbSdk", "onDownloadProgress -->下载X5内核进度：" + i);
                    }
                });

        QbSdk.PreInitCallback cb =
                new QbSdk.PreInitCallback() {
                    @Override
                    public void onViewInitFinished(boolean arg0) {
                        // x5內核初始化完成的回调，true表x5内核加载成功，否则表加载失败，会自动切换到系统内核。
                        Log.d("QbSdk", " 内核加载 " + arg0);
//                        mIsInitTBSSuccess = arg0;
//                        if (!mIsInitTBSSuccess) {
//                            QbSdk.reset(getApplicationContext());
//                            TbsDownloader.startDownload(getApplicationContext());
//                        }
//                        Log.d("QbSdk", "mIsInitTBSSuccess = " + mIsInitTBSSuccess);
                    }

                    @Override
                    public void onCoreInitFinished() {
                        //内核初始化完毕
                        Log.d("QbSdk", "内核初始化完毕");
                    }
                };

        // x5内核初始化接口
        QbSdk.initX5Environment(getApplicationContext(), cb);
        //Log.i("QbSdk", "是否可以加载X5内核: " + QbSdk.canLoadX5(this));
        Log.i("QbSdk", "app是否主动禁用了X5内核: " + QbSdk.getIsSysWebViewForcedByOuter());
    }

}